//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    phone: "",
    card: '',
    hidden1: false,
    hidden2: true,
    texts: ''
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    console.log('onLoad')
    var that = this
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function (userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      })
    })
  },
  yzm: function (e) {
    this.setData({
      phone: e.detail.value
    })
  }, card: function (e) {
    this.setData({
      card: e.detail.value
    })
  },
  formSubmit: function (e) {
    var yzm = wx.getStorageSync('smsCode')
    var phone = this.data.phone
    if (phone != e.detail.value.phone) {
      wx.showToast({
        title: '输入的手机号与验证号码不一致!',
        icon: 'loading',
        duration: 2000
      })
      return false;
    } else if (e.detail.value.phone == '') {
      wx.showToast({
        title: '手机号码不能为空!',
        icon: 'loading',
        duration: 2000
      })
      return false;
    } else if (!(/^1[34578]\d{9}$/.test(e.detail.value.phone))) {
      wx.showToast({
        title: '手机格式不正确!',
        icon: 'loading',
        duration: 2000
      })
      return false;
    } else if (e.detail.value.yzm == '' || e.detail.value.yzm != yzm) {
      wx.showToast({
        title: '验证码填写错误',
        icon: 'loading',
        duration: 2000
      })
      return false;
    }
    var that = this
    wx.request({
      url: app.globalData._new_url + 'MenuAPI/Member/Add',
      data: {
        ShopId: app.globalData.shopids,
        CardNumber: this.data.card,
        Name: '',
        Password: '123456',
        Mobile: phone,
        CreateUserId: app.globalData.openId,
        OpenId: app.globalData.openId,
      },
      method: 'POST',
      success: function (res) {
        console.log("kt", res)
        wx.showModal({
          title: '温馨提示',
          content: res.data.message,
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              wx.switchTab({
                url: '../hyzxm/hyzxm',
              })
            }
          }
        })
      }
    })

  },
  getYzm: function () {
    var phone = this.data.phone
    var that = this
    if (phone == '') {
      wx.showToast({
        title: '手机号码不能为空!',
        icon: 'loading',
        duration: 2000
      })
      return false;
    } else if (!(/^1[34578]\d{9}$/.test(phone))) {
      wx.showToast({
        title: '手机格式不正确!',
        icon: 'loading',
        duration: 2000
      })
      return false;
    }

    wx.request({
      url: app.globalData._new_url + 'MenuAPI/Member/CheckPhoneNumber',
      data: { shopId: app.globalData.shopids, phoneNumber: phone },
      method: 'POST',
      success: function (res) {
        console.log(res)
        if (res.data.status == 0) {
          that.setData({
            hidden1: true,
            hidden2: false,
            texts: 60
          })
          that.countdown(that.data.texts)
          wx.request({
            url: app.globalData._new_url + 'MenuAPI/ApiSms/SmsSender',
            data: {
              ShopId: app.globalData.shopids,
              phoneNumber: phone,
              userId: app.globalData.openId,
            },
            method: 'POST',
            success: function (res) {
              console.log("this", res)
              wx.setStorageSync('smsCode', res.data.Code)
            }
          })

        } else {
          wx.showModal({
            title: '温馨提示',
            content: res.data.message,
            showCancel: false
          })
        }
      }
    })

  },
  countdown: function (ob) {
    var that = this
    var second = ob
    if (second == 0) {
      console.log("Time Out...");
      that.setData({
        texts: "",
        hidden2: true,
        hidden1: false,
      });
      return;
    }
    var time = setTimeout(function () {
      that.setData({
        texts: second - 1
      });
      that.countdown(that.data.texts);
    }
      , 1000)
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.dpxinxi.sname,
      path: '/pages/index/index',
      success: function (res) {
        // 分享成功
      }
    }
  }
})
